package com.wthink.card;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.annotation.Nullable;

import com.wthink.card.Thread.SerialFrameProcThread;
import com.wthink.card.Thread.SocketFrameProcThread;
import com.wthink.card.models.ShdFrameBundle;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import static com.wthink.hzgj.Constants.UI_RC522;

public class AlbumsActivity extends Activity {

    private SocketFrameProcThread socketFrameThread;
    private SerialFrameProcThread serialFrameThread;

   //public static final int UI_RC522 = 3;    //刷卡卡号
    //public static StringBuilder mStrCardId = new StringBuilder();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		/*if(socketFrameThread == null)
		{
			socketFrameThread = SocketFrameProcThread.getInstance(this, uiHandler);

			socketFrameThread.start();
		}*/
        if (serialFrameThread == null) {
            serialFrameThread = SerialFrameProcThread.getInstance(this, uiHandler);

            serialFrameThread.start();
        }

    }

    final Handler uiHandler = new Handler() {

        public void handleMessage(Message msg) {
            try {
                switch (msg.what) {
                    case UI_RC522:
                        Log.d("TAGGGGGGG", "刷卡ID-->" + SocketFrameProcThread.mStrCardId);
                        ByteBuffer frameBuf = ByteBuffer.allocate(20);
                        if (SocketFrameProcThread.mStrCardId != null) {
                            frameBuf.put((byte) 0x1b);
                            frameBuf.put((byte) 8);
                            frameBuf.put(SocketFrameProcThread.mStrCardId.toString().getBytes());
                            frameBuf.put((byte) 0x1c);
                            frameBuf.put((byte) 0x00);
                        }

                        break;

                    default:
                        super.handleMessage(msg);
                }
            } catch (Exception e) {
                Log.e("AlbumsActivity", "Handler Exception ");
                e.printStackTrace();
            }
        }
    };


    public class PersecProcThread extends ShdThread {
        //String msg = "{\"type\":\"0b01\",\"payload\":\"AAAAATEyMzQ1AA==\",\"sync\":1,\"timeout\":\"5\"}";
        String msg = "SENDDATA";
        ByteBuffer frameBuf = ByteBuffer.allocate(250);

        public PersecProcThread(Context ctx) {
            super(ctx, "");
        }

        public void sendMsgToHandler(int what) {
            uiHandler.sendMessage(uiHandler.obtainMessage(what));
        }

        public void run() {
            try {
                int i = 0;
                while (!isInterrupted()) {
                    if (i % 3 == 0) {
                        //心跳包
                        //操作屏升级状态下不发送

                        byte[] data = msg.toString().getBytes(Charset.forName("GB2312"));
                        //ByteBuffer frameBuf = ByteBuffer.allocate(16);
                        //frameBuf.put(msg.getBytes(Charset.forName("GB2312")));

                        if (serialFrameThread != null && SerialFrameProcThread.GetScreenWorkMode() == SerialFrameProcThread.SCREEN_WORK_MODE_NORMAL) {
                            serialFrameThread.SendToMainBoardNormalFrm(ShdFrameBundle.CFS_KPLINE, frameBuf.array(), frameBuf.position());
                            //serialFrameThread.SendToMainBoardNormalFrm(0x01, data, data.length);
                        }
                    }
                    //喂狗
                    if (i % 10 == 0) {
                        if (socketFrameThread != null) {
                            frameBuf.clear();
                            frameBuf.put((byte) 7);
                            frameBuf.put((byte) 120);
                            if (socketFrameThread != null)
                                socketFrameThread.SendToMainBoardNormalFrm(ShdFrameBundle.B_SOCKET_FRAME_01, frameBuf.array(), 2, ShdFrameBundle.A_SOCKET_MODEL_01);
                        }
                    }
                    i++;
                    Thread.sleep(200);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
